/**
 * Created with IntelliJ IDEA
 * Description:
 * User: YKH
 * Date: 2022-05-03
 * Time: 23:14
 * author:MyArraysList
 */
public class MyArraysList {
     Node head;
     static class Node {
        private int val;
        private Node next;

        public Node(int val) {
            this.val = val;
        }
    }

    //创建链表
    public void createLink() {
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        Node node4 = new Node(4);
        Node node5 = new Node(5);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
    }

    //打印链表
    public void display() {
         Node cur = head;
         while( cur != null) {
             System.out.print(cur.val + " ");
             cur = cur.next;
         }
    }

    //头插法
    public void addHead(int data) {
         Node newnode = new Node(data);
         newnode.next = head;
         head = newnode;
    }

    //尾插法
    public void addRear(int data) {
        Node newnode = new Node(data);
        newnode.next = null;
         if(head == null) {
             head = newnode;
             return ;
         }
        Node cur = head;
        while( cur.next != null) {
            cur = cur.next;
        }
        cur.next = newnode;
    }


}
